package com.ruoyi.common.utils.file;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class FileContentTypeUtils {
    private static final Map<String, String> EXT_TO_CONTENT_TYPE = new HashMap<>();

    static {
        // 图片
        EXT_TO_CONTENT_TYPE.put("jpg", "image/jpeg");
        EXT_TO_CONTENT_TYPE.put("jpeg", "image/jpeg");
        EXT_TO_CONTENT_TYPE.put("png", "image/png");
        EXT_TO_CONTENT_TYPE.put("gif", "image/gif");
        EXT_TO_CONTENT_TYPE.put("bmp", "image/bmp");
        EXT_TO_CONTENT_TYPE.put("webp", "image/webp");

        // 视频
        EXT_TO_CONTENT_TYPE.put("mp4", "video/mp4");
        EXT_TO_CONTENT_TYPE.put("mov", "video/quicktime");
        EXT_TO_CONTENT_TYPE.put("avi", "video/x-msvideo");
        EXT_TO_CONTENT_TYPE.put("flv", "video/x-flv");
        EXT_TO_CONTENT_TYPE.put("wmv", "video/x-ms-wmv");

        // 音频
        EXT_TO_CONTENT_TYPE.put("mp3", "audio/mpeg");
        EXT_TO_CONTENT_TYPE.put("wav", "audio/wav");
        EXT_TO_CONTENT_TYPE.put("amr", "audio/amr");
        EXT_TO_CONTENT_TYPE.put("aac", "audio/aac");
        EXT_TO_CONTENT_TYPE.put("ogg", "audio/ogg");

        // 文档
        EXT_TO_CONTENT_TYPE.put("pdf", "application/pdf");
        EXT_TO_CONTENT_TYPE.put("doc", "application/msword");
        EXT_TO_CONTENT_TYPE.put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        EXT_TO_CONTENT_TYPE.put("xls", "application/vnd.ms-excel");
        EXT_TO_CONTENT_TYPE.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        EXT_TO_CONTENT_TYPE.put("ppt", "application/vnd.ms-powerpoint");
        EXT_TO_CONTENT_TYPE.put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");

        // 文本
        EXT_TO_CONTENT_TYPE.put("txt", "text/plain");
        EXT_TO_CONTENT_TYPE.put("csv", "text/csv");
        EXT_TO_CONTENT_TYPE.put("xml", "text/xml");
        EXT_TO_CONTENT_TYPE.put("html", "text/html");
        EXT_TO_CONTENT_TYPE.put("json", "application/json");

        // 压缩包
        EXT_TO_CONTENT_TYPE.put("zip", "application/zip");
        EXT_TO_CONTENT_TYPE.put("rar", "application/x-rar-compressed");
        EXT_TO_CONTENT_TYPE.put("7z", "application/x-7z-compressed");
        EXT_TO_CONTENT_TYPE.put("tar", "application/x-tar");
        EXT_TO_CONTENT_TYPE.put("gz", "application/gzip");

        // 其他常见类型
        EXT_TO_CONTENT_TYPE.put("exe", "application/octet-stream");
        EXT_TO_CONTENT_TYPE.put("dll", "application/octet-stream");
        EXT_TO_CONTENT_TYPE.put("apk", "application/vnd.android.package-archive");
        EXT_TO_CONTENT_TYPE.put("ipa", "application/octet-stream");
    }

    /**
     * 根据文件扩展名获取 ContentType
     * @param fileExt 文件扩展名（如 "jpg"、"mp4"）
     * @return 对应的 MIME 类型，默认返回 "application/octet-stream"
     */
    public static String getContentType(String fileExt) {
        if (StringUtils.isBlank(fileExt)) {
            return "application/octet-stream";
        }
        // 去掉 "."（如 ".jpg" -> "jpg"）
        String ext = fileExt.replace(".", "").toLowerCase();
        return EXT_TO_CONTENT_TYPE.getOrDefault(ext, "application/octet-stream");
    }
}
